<?php

namespace app\admin\controller;

use think\Config;
use think\Controller;
use think\Loader;
use think\Request;

class Permissions extends Auth
{
    private $pageNum;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('id')!='1'){
            $this->error('没有权限');
        }
        $this->pageNum=Config::get('admin.pageNum');
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function rule_list()
    {
        $field=db( 'auth_rule' )->order('id desc')->paginate( $this->pageNum );
        $this->assign('field',$field);
        $this->assign('title','规则列表');
        return view();

    }

    /**
     * 添加规则
     *
     * @return \think\Response
     */
    public function rule_store()
    {
        if(Request::instance()->isPost()){
//            p(input('post.'));die;
            $data=input('post.');

            $validate = Loader::validate('authrule');
            //如果验证不通过
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $result=db( 'auth_rule' )->insert($data);
            if(!$result){
                $this->error('添加失败');die;
            }
            $this->success('添加成功');die;
        }
        //
        $this->assign('title','添加规则');
        return view();
    }

    /**
     * @info 删除节点
     */
    public function rule_del(){
        $id=input('post.id',0,'intval');
        if(!$id){
            returnMsg(0,'param error');
        }
        $where['id']=$id;
        $re=db('auth_rule')->where($where)->delete();
        if(!$re){
            returnMsg(0,'error');
        }
        returnMsg(1,'success');
    }

    /*
     * @info 角色编辑
     */
    public function rule_edit(){
        if(Request::instance()->isPost()){
            $data=input('post.');
            if(!isset($data['id'])||!$data['id']){
                $this->error('参数不全操作失败');die;
            }

            $validate = Loader::validate('authrule');
            //如果验证不通过
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
//            p($data);die;
            $re=db('auth_rule')->update($data);
            if(!$re){
                $this->error('修改失败');die;
            }
            $this->success('修改成功');die;
        }


        $id=input('get.id',0,'intval');
        $where['id']=$id;
        $field=db('auth_rule')->where($where)->find();
        $this->assign('field',$field);
        $this->assign('title','规则列表');
        return view();
    }

    /**
     * @info 搜索节点
     */
    public function rule_search(){
        $name=input('post.name','');
        if(!$name){
            $this->error('请输入搜索的节点名');
        }
        $where['name']=$name;
        $data=db('auth_rule')->where($where)->paginate($this->pageNum);
        $this->assign('field',$data);
        $this->assign('title','搜索节点');
        return view('rule_list');
    }


    /**
     * 用户组模块
     */
    /**
     * @info 查看所有组列表
     */
    public function group_list(){
        $field=db( 'auth_group' )->order('id desc')->paginate( $this->pageNum );
//        p($field);die;

//        获取所有节点名数据
        $rulesData=db('auth_rule')->where(['status'=>1])->column('id,title','id');
        $rulesArr=array();
        $resetData=array();
        foreach($field->items() as $k=>$v){
            $rulesArr=explode(',',$v['rules']);
            foreach($rulesArr as $rk=>$rv){
                $rulesArr[$rk]=$rulesData[$rv];
            }
            $re=implode(',',$rulesArr);
            $resetData[$k]=$v;
            $resetData[$k]['rules']=$re;
        }
            $field->itemslist=$resetData;
        $this->assign('field',$field);
        $this->assign('title','用户组列表');
        return view();
    }

    /**
     * @info 用户组成员详情
     */
    public function group_detail(){
        if(Request::instance()->isPost()){
            $data=input('post.');
            if(!isset($data['uid'])||!$data['uid']||!isset($data['group_id'])||!$data['group_id']){
                $this->error('param error');die;
            }
            $count=db('auth_group_access')->where($data)->find();
            if($count){
                $this->error('该用户已添加');die;
            }
            $re=db('auth_group_access')->insert($data);
            if(!$re){
                $this->error('添加失败');die;
            }
            $this->success('添加成功');die;
        }

//        获取当前组的所有id
        $gid=input('get.gid',0,'intval');
        $where['group_id']=$gid;
        $ids=db('auth_group_access')->where($where)->column('uid');
//        列出当前组所有用户
        $field=db('user')->where('id','in',$ids)->where('is_lock=0')->order('id desc')->field('id,username,email')->select();

//        快速添加用户
        $userData=db('user')->where('is_lock=0 AND id!=1')->field('id,username')->select();
        $this->assign('userData',$userData);
        $this->assign('field',$field);
        $this->assign('title','用户组列表');
        return view();
    }

    /**
     * @info 搜索用户详情
     */
    public function group_detail_search(){
        $search=input('get.search','');
        if(!$search){
            $this->error('param error');die;
        }
        $where['username']=$search;
        $field=db('user')->where($where)->paginate(1);
        $userData=db('user')->where('is_lock=0 AND id!=1')->field('id,username')->select();
        $this->assign('userData',$userData);
        $this->assign('field',$field);
        $this->assign('title','用户组列表');
        return view('group_detail');
    }




    /**
     * @info 添加组
     */
    public function group_store(){

        if(Request::instance()->isPost()){
            $data=input('post.');

            $validate = Loader::validate('authgroup');
            //如果验证不通过
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $data['rules']=implode(',',$data['rules']);
            $result=db( 'auth_group' )->insert($data);
            if(!$result){
                $this->error('添加失败');die;
            }
            $this->success('添加成功');die;
        }



        $tempData=db('auth_rule')->where(['status'=>1])->field('id,title')->select();
//        p($ruleData);die;
        $resetData=array();
        foreach($tempData as $k=>$v){
            $node=explode('-',$v['title']);
            $resetData[$k]=$v;
            $resetData[$k]['title']=$node[1];
            $resetData[$k]['group']=$node[0];
        }
        $ruleData=array();
//        重组数据
        foreach($resetData as $kk=>$vv){
            $ruleData[$vv['group']][]=$vv;
        }
//        p($ruleData);die;
        $this->assign('ruleData',$ruleData);
        return view();
    }

    /**
     * @Info 编辑组
     */
    public function group_edit(){
        if(Request::instance()->isPost()){
            $data=input('post.');

            $validate = Loader::validate('authgroup');
            //如果验证不通过
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $data['rules']=implode(',',$data['rules']);
            $result=db( 'auth_group' )->update($data);
            if(!$result){
                $this->error('修改失败');die;
            }
            $this->success('修改成功');die;
        }
        $tempData=db('auth_rule')->where(['status'=>1])->field('id,title')->select();
//        p($ruleData);die;
        $resetData=array();
        foreach($tempData as $k=>$v){
            $node=explode('-',$v['title']);
            $resetData[$k]=$v;
            $resetData[$k]['title']=$node[1];
            $resetData[$k]['group']=$node[0];
        }
        $ruleData=array();
//        重组数据
        foreach($resetData as $kk=>$vv){
            $ruleData[$vv['group']][]=$vv;
        }


        $gid=input('get.gid',0,'intval');
        if(!$gid){
            $this->error('param error');die;
        }
        $where['id']=$gid;
        $field=db('auth_group')->where($where)->find();
        $field['rules']=explode(',',$field['rules']);
//        $oldData=db('auth_group_access')
        $this->assign('ruleData',$ruleData);
        $this->assign('field',$field);

        return view();
    }

    /**
     * @Info 删除组
     */
    public function group_del(){

    }






}
